// pages/orderlist/orderlist.js
const { http } = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuIdx:0,
    list:[],
    total:0,
    isShowConfirm: false,
    walletPsd:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      menuIdx:options.menuIdx,
    })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getList(){
    let order_type = this.renderOrderType();
    http('/order/get_order', { order_type:order_type,start:0}).then( res => {
      if(res.code == 1){
        console.log(res);
        var list = res.data.map(function(v){
          let vtotal = v.order_detail.reduce((total, val) => total += val.goods_num,0);
          v.vtotal = vtotal;
          return v
        })
        this.setData({
          list:this.data.list.concat(list),
          total:this.data.total,
        }) 
        console.log(this.data.list)
      }
    })
  },
  //根据menuIdx 生成  传参
  renderOrderType(){
    return ['0',"1","2","3","4","11"][this.data.menuIdx];
  },
  /**
   * 切换类型
  */
  handertype(e){
    let idx = e.currentTarget.dataset.idx;
    let type = e.currentTarget.dataset.type;
    this.setData({
      menuIdx:idx,
      list:[],
    });
    this.getList();
  },
  /**
   * 支付
  */
  goPay(){
    this.setData({
      isShowConfirm: true,
    })
  },
  /**
   * 申请退款
  */
  applyRefund(){
    this.setData({
      isShowConfirm:true,
    })
  },
  setValue: function (e) {
    this.setData({
      walletPsd: e.detail.value
    })
  },
  cancel: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },
  confirmAcceptance: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
      walletPsd:"",
    })
  },
  //确认收货
  confirmHuo:function(){
    let order_no = "";
    wx.showModal({
      title: '确认',
      content: '请确认收货',
      success(res){
        if(res.confirm){
          wx.showLoading();
          http('/order/confirm_receive', { order_no: order_no}).then(res => {
            wx.hideLoading();
            if(res.code == 1){
              this.setData({
                list:[],
              });
              this.getList()
            }
          })
        }
      }
    })
  },
  //去评价
  toEval:function(config){
    let { order_no, order_id, goods_id, goods_attr_id } = config;
    wx.navigateTo({
      url: `/pages/ordereval/ordereval?order_id=${order_id}&order_no=${order_no}&goods_id=${goods_id}&goods_attr_id=${goods_attr_id}`,
    })
  },
  bindPickerChange(e){
    let pid = e.currentTarget.dataset.idx;
    let cid = e.detail.value;
    //整理跳转数据
    let order_no = e.currentTarget.dataset.order_no;
    let order_id = this.data.list[pid].order_detail[cid].order_id;
    let goods_id = this.data.list[pid].order_detail[cid].goods_id;
    let goods_attr_id = this.data.list[pid].order_detail[cid].goods_attr_id;
    this.toEval({
      order_no, order_id, goods_id, goods_attr_id
    })
  },
  //删除订单
  deleteOrder(){
    let order_no = "";
    wx.showModel({
      title: '提示',
      content: '确认要删除',
      success(res){
        if(res.confirm){
          wx.showLoading();
          http('/order/del_order', { order_no: order_no}).then(res => {
            wx.hideLoading();
            if(res.code == 1){
              this.setData({
                list: [],
              })
              this.getList()
            }
          })
        }
      }
    })
  },
  //订单详情
  toOrderDtl(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../orderdetail/orderdetail?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})